package com.ruoyi.web.controller.fx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.fx.FxOrder;
import com.ruoyi.system.domain.fx.FxUser;
import com.ruoyi.system.domain.fx.FxWithdrawalRecord;
import com.ruoyi.system.domain.vo.FxMoneyVo;
import com.ruoyi.system.service.fx.IFxOrderService;
import com.ruoyi.system.service.fx.IFxUserService;
import com.ruoyi.system.service.fx.IFxWithdrawalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 提现记录表Controller
 *
 * @author ruoyi
 * @date 2024-07-30
 */
@RestController
@RequestMapping("/fx/withdrawal/record")
public class FxWithdrawalRecordController extends BaseController {
    @Autowired
    private IFxWithdrawalRecordService fxWithdrawalRecordService;
    @Autowired
    private IFxUserService iFxUserService;

    @Autowired
    private IFxOrderService fxOrderService;

    /**
     * 查询提现记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxWithdrawalRecord fxWithdrawalRecord) {
        startPage();
        List<FxWithdrawalRecord> list = fxWithdrawalRecordService.selectFxWithdrawalRecordList(fxWithdrawalRecord);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxWithdrawalRecord fxWithdrawalRecord) {
        List<FxWithdrawalRecord> list = fxWithdrawalRecordService.selectFxWithdrawalRecordList(fxWithdrawalRecord);
        ExcelUtil<FxWithdrawalRecord> util = new ExcelUtil<FxWithdrawalRecord>(FxWithdrawalRecord.class);
        util.exportExcel(response, list, "提现记录数据");
    }


    /**
     * 获取可提现信息
     */
    @Log(title = "获取可提现信息", businessType = BusinessType.OTHER)
    @GetMapping("/getAblewithdrawal")
    public AjaxResult getAblewithdrawal() {
        List<Long> roles =  SecurityUtils.getLoginUser().getUser().getRoles()
                .stream().map(SysRole::getRoleId).collect(Collectors.toList());
        if (roles.contains(1L) || roles.contains(6L)) {
            return success();
        }
        FxUser fxUser = iFxUserService.selectFxUserByUserId(SecurityUtils.getUserId());
        FxOrder fxorder = new FxOrder();
        if(fxUser.getRoleType() == 4){
            fxorder.setOrderType(2L);
            fxorder.setStoreId(fxUser.getStoreId());
        }
        FxMoneyVo fxMoneyVo = fxOrderService.getTotalMoney(fxorder);
        return success(fxMoneyVo);
    }

    /**
     * 获取提现记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(fxWithdrawalRecordService.selectFxWithdrawalRecordById(id));
    }

    /**
     * 新增提现记录
     */
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxWithdrawalRecord fxWithdrawalRecord) {
        fxWithdrawalRecord.setCreateBy(getUsername());
        fxWithdrawalRecord.setUserId(Convert.toInt(getUserId()));
        return toAjax(fxWithdrawalRecordService.insertFxWithdrawalRecord(fxWithdrawalRecord));
    }

    /**
     * 修改提现记录
     */
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxWithdrawalRecord fxWithdrawalRecord) {
        return toAjax(fxWithdrawalRecordService.updateFxWithdrawalRecord(fxWithdrawalRecord));
    }

    /**
     * 批量审核
     *
     * @return
     */
    @GetMapping("/audit")
    public AjaxResult audit(@RequestParam Integer[] ids, @RequestParam Long status) {
        return toAjax(fxWithdrawalRecordService.audit(ids, status));
    }

    /**
     * 删除提现记录
     */
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(fxWithdrawalRecordService.updateDelFlag(ids));
    }
}
